<?php
namespace Admin\Controller;

use Think\Controller;

class OrderController extends Controller
{
    //empty control
    public function _empty()
    {
        $this->display("Basic/404");
    }

    /*
     * 商家订单首页
     * ＠wan
     * */
    public function index()
    {
        $uid = session("uid");
        if ($uid == null || $uid == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
                return;
            } else {
                $this->display("User/index");
                return;
            }
        }
        $shop = M("shop_info");
        $order = M("order_main");
        $user = M("user_main");
        $userArr = $shop->where("uid=" . $uid)->find();
        if (empty($userArr)) {
            $this->assign("code", 3002);
            $this->assign("desc", "您还没有店铺，赶紧创建吧");
            $this->display("shopOrderList");
            return;
        }
        $orderArr = $order->where("shop_id=" . $userArr['shop_id'])->select();
        if (empty($orderArr)) {
            $this->assign("code", 3002);
            $this->assign("desc", "您还没有订单");
        } else {
            $datas = [];
            foreach ($orderArr as $o) {
                $res = $user->where("uid=" . $o['uid'])->find();
                $o['name'] = $res['name'];
                $o['phone'] = $res['phone'];
                $datas[] = $o;
            }
            $this->assign("datas", $datas);
        }
        $this->display("shopOrderList");
    }

//订单首页
    public function order()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {

            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $obj2 = M("order_main");
            $usermain = M("user_main");
            $arr = $usermain->where("phone=" . $user)->find();//查询用户的角色
            if ($arr['role'] == 2) {
                $shop = M("shop_info");
                $shopRes = $shop->where("uid=" . $arr["uid"])->select();
                $counts = [];
                foreach ($shopRes as $s) {
                    $order_count = $obj2->where("shop_id =" . $s["shop_id"] . " and status=1")->count();//商家订单数
                    $counts[] = $order_count;
                }
                $order_sum = array_sum($counts);
                $this->assign("data", $order_sum);
                if (isMobile()) {
                    $this->display("order_m");
                } else {
                    $this->display();
                }
            }
//            else {
//                $data["paidang_count"] = $obj->where("belong is not null and status is null")->count();
//                $data["liangyi_count"] = $obj2->where("status=1")->count();
//                $data["end_count"] = $obj2->where("status=2 or status=6")->count();
//                $data["startps_count"] = $obj2->where("status=3")->count();
//                $data["endps_count"] = $obj2->where("status=4")->count();
//                $data["fg_count"] = $obj3->where("status=5")->count();
//            }

//            $this->assign("data", $data);
//            if (isMobile()) {
//                $this->display("order_m");
//            } else {
//                $this->display();
//            }
        }
    }

    //已预约
    public function orderList()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $obj = M("ming_yuyue");
//            $userm = M("user_main");
//            $arr = $userm->where("name='" . $user . "'")->find();//查询用户的角色
//            if ($arr['role'] != 0) {
//                $count = $obj->where("belong is null and rel=" . $arr['uid'])->count();
//                $p = getPage($count);
//                $list = $obj->where("belong is null and rel=" . $arr['uid'])->order("updated desc")->limit($p->firstRow, $p->listRows)->select();
//            } else {
                $count = $obj->count();
                $p = getPage($count);
                $list = $obj->order("create_time desc")->limit($p->firstRow, $p->listRows)->select();
//            }

            $this->assign('datas', $list);
            $this->assign('page', $p->show());
            if (isMobile()) {
                $this->display("orderList_m");
            } else {
                $this->display();
            }
        }
    }

    //已派单
    public function orderList3()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $obj = M("user_record");
            $obj2 = M("user_main");
            $role = session("role");
            if ($role == 2) {
                $count = $obj->where("belong='" . $user . "' and status is null")->count();
                $p = getPage($count);
                $list = $obj->where("belong='" . $user . "' and status is null")->order("updated desc")->limit($p->firstRow, $p->listRows)->select();
            } else {
                $count = $obj->where("belong is not null and status is null")->count();
                $p = getPage($count);
                $list = $obj->where("belong is not null and status is null")->limit($p->firstRow, $p->listRows)->select();
            }
            //时间排序
            $datas = [];
            foreach ($list as $l) {
                $data["id"] = $l["id"];
                $data["phone"] = $l["phone"];
                $mainArr = $obj2->where("phone=" . $data["phone"])->find();
                $data["uid"] = $mainArr["uid"];
                $data["xin"] = $l["xin"];
                $data["company"] = $l["company"];
                $data["yuyue_date"] = $l["yuyue_date"];
                $data["yuyue"] = $l["yuyue"];
                $data["updated"] = $l["updated"];
                $data["belong"] = $l["belong"];
                $data["sale"] = $l["sale"];
                $data["addr"] = $l["addr"];
                $data["descr"] = $l["descr"];

                $yuyue_date = $l["yuyue_date"];
                $yuyue = $l["yuyue"];
                $point = substr($yuyue, 0, 2);
                $str = substr($point, -1);
                if ($str == ":") {
                    $point = substr($yuyue, 0, 1);
                }
                $yuyue_time = $yuyue_date . " " . $point . ":00";
                $data["point"] = strtotime($yuyue_time);
                $datas[] = $data;
            }
            $arr2 = array();
            foreach ($datas as $d) {
                $arr2[] = $d["point"];
            }
            array_multisort($arr2, SORT_ASC, $datas);
            $this->assign('datas', $datas);
            $this->assign('page', $p->show());

            if (isMobile()) {
                $this->display("orderList3_m");
            } else {
                $this->display();
            }
        }
    }

    //为分配工厂的订单列表
    public function orderList2()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $obj = M("order_main");
            $obj2 = M("user_main");
            $role = session("role");
            $arr = $obj2->where("name='" . $user . "'")->find();

            if ($role == 2) {
                $count = $obj->where("status = 1 and belong_gc is null and rel=" . $arr['uid'])->count();
                $p = getPage($count);
                $list = $obj->where("status = 1 and belong_gc is null and rel=" . $arr['uid'])->order("order_time desc")->limit($p->firstRow, $p->listRows)->select();
            } else {
                $count = $obj->where("status = 1 and belong_gc is null")->count();
                $p = getPage($count);
                $list = $obj->where("status = 1 and belong_gc is null")->order("order_time desc")->limit($p->firstRow, $p->listRows)->select();
            }

            $datas = [];
            foreach ($list as $l) {
                $data["uid"] = $l["uid"];
                $arr = $obj2->where("uid=" . $data["uid"])->find();
                $data["name"] = $arr["name"];
                $data["phone"] = $arr["phone"];
                $data["orderNum"] = $l["ordernum"];
                $data["count"] = $l["count"];
                $data["price_count"] = $l["price_count"];
                $data["dj_count"] = $l["dj_count"];
                $data["order_time"] = $l["order_time"];
                $data["status"] = $l["status"];
                $data["omid"] = $l["omid"];
                $data["belong"] = $l["belong"];
                $data["belong_gc"] = $l["belong_gc"];
                $datas[] = $data;
            }
            $status = 1;
            $this->assign('status', $status);
            $this->assign('datas', $datas);
            $this->assign("page", $p->show());
            if (isMobile()) {
                $this->display("orderList2_m");
            } else {
                $this->display();
            }
        }
    }

    //订单编辑
    public function orderEdit()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $obj = M("pro_color");
            $datas = $obj->select();
            $this->assign("datas", $datas);
            if (isMobile()) {
                $this->display("orderEdit_m");
            } else {
                $this->display();
            }
        }
    }

    //订单编辑_衬衫
    public function orderEdit2()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $phone = $_POST['phone'];
            $color = $_POST["color"];
            $collar_type = $_POST["collar_type"];
            $sleeve_type = $_POST["sleeve_type"];
            $front_fly = $_POST["front_fly"];
            $pocket = $_POST["pocket"];
            $select = $_POST["select"];
            $descr2 = $_POST["descr2"];
            $lts = $_POST["lts"];
            $num = $_POST["num"];
            $xf_type = $_POST["xf_type"];
            $sex = $_POST["sex"];
            $zuo = $_POST["zuo"];
            $price = I("post.price");
            $dj = I("post.dj");
            if (isset($phone)) {
                $timestamp = time();
                $obj = M("user_main");
                $arr = $obj->where("phone=" . $phone)->find();
                $uid = $arr["uid"];
                //查询
                $orderm = M("order_main");
                $orderp = M("order_profile");
//                $orderPay = M("order_money");
                $data2['uid'] = $uid;
                $data2['order_time'] = $timestamp;
                $data2['pro_id'] = 1;
                $data2['color'] = $color;
                $data2['collar_type'] = $collar_type;
                $data2['sleeve_type'] = $sleeve_type;
                $data2["front_fly"] = $front_fly;
                $data2["pocket"] = $pocket;
                $data2['type'] = $select;
                $data2['descr2'] = $descr2;
                $data2['status'] = 1;
                $data2["belong"] = $lts;
                $data2['num'] = $num;
                $data2["xf_type"] = $xf_type;
                $data2["sex"] = $sex;
                $data2["zuo"] = $zuo;
                $data2["price"] = $price;
                $data2["dj"] = $dj;
                $data2["updated"] = $timestamp;
                $rs3 = $orderm->where("DATE(FROM_UNIXTIME(order_time))=CURDATE() and uid=" . $uid)->find();
                $rs = $orderm->where("DATE(FROM_UNIXTIME(order_time))=CURDATE()")->count();
                $rq = date("Ymd", $timestamp);
                if (empty($rs3)) {
                    $str = $rs + 1;
                    $var = sprintf("%04s", $str);
                    $rs2 = $orderp->where("DATE(FROM_UNIXTIME(order_time))=CURDATE() and uid=" . $uid)->count();
                    $str2 = $rs2 + 1;
                    $var2 = sprintf("%03s", $str2);
                    $data2["orderNum2"] = $rq . $var . "-" . $var2;
                    $data["order_time"] = $timestamp;
                    $data["orderNum"] = $rq . $var;
                    $data["uid"] = $uid;
                    $data["count"] = $num;
                    $data["price_count"] = $price;
                    $data["dj_count"] = $dj;
                    $data["status"] = 1;
                    $data["belong"] = $lts;
                    $data["updated"] = $timestamp;
                    $omid = $orderm->add($data);
                } else {
                    $orderNum = $rs3["ordernum"];
                    $omid = $rs3["omid"];
                    $count = $rs3["count"] + $num;
                    $price_count = $rs3["price_count"] + $price;
                    $dj_count = $rs3["dj_count"] + $dj;
                    $orderm->count = $count;
                    $orderm->price_count = $price_count;
                    $orderm->dj_count = $dj_count;
                    $orderm->updated = $timestamp;
                    $orderm->where("omid=" . $omid)->save();
                    $rs2 = $orderp->where("DATE(FROM_UNIXTIME(order_time))=CURDATE() and uid=" . $uid)->count();
                    $str2 = $rs2 + 1;
                    $var2 = sprintf("%03s", $str2);
                    $data2["orderNum2"] = $orderNum . "-" . $var2;
                }
                $data2["omid"] = $omid;
                $oid = $orderp->add($data2);
                $data3['oid'] = $oid;
                $data3['omid'] = $omid;
                $data3['pay_status'] = 1;
//                $orderPay->add($data3);
                if (!empty($oid)) {
                    $obj2 = M("user_record");
                    $obj2->status = 2;
                    $obj2->where("uid=" . $uid)->save();
                    $desc = "订单添加成功！";
                    $this->assign("desc", $desc);
                    $this->assign("phone", $phone);
                    $this->assign("lts", $lts);
                    $this->display("orderEdit");
                } else {
                    $desc = "订单添加失败！";
                    $this->assign("desc", $desc);
                    $this->assign("phone", $phone);
                    $this->assign("lts", $lts);

                    if (isMobile()) {
                        $this->display("orderEdit_m");
                    } else {
                        $this->display("orderEdit");
                    }
                }
            }
        }
    }

    //预约、派单搜索
    public function search()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $select = $_POST["select"];
            $ifin = $_POST["ifin"];
            if ($ifin != '') {
                if ($select == "phone") {
                    $record = M("user_record");
                    $datas = $record->where("phone like '" . $ifin . "%'")->select();
                    $this->assign('datas', $datas);
                    if (isMobile()) {
                        $this->display("orderList_m");
                    } else {
                        $this->display("orderList");
                    }
                }
            } else {
                $desc = "请输入搜索内容";
                $this->assign("desc", $desc);
                if (isMobile()) {
                    $this->display("orderList_m");
                } else {
                    $this->display("orderList");
                }
            }
        }
    }

    //订单搜索
    public function orderSearch($id)
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $select = $_POST["select"];
            $ifin = $_POST["ifin"];
            $status = $_POST["status"];
            $obj = M("order_profile");
            $obj2 = M("user_main");
            if (isset($ifin)) {
                if ($select == "lts") {
                    if ($id == 2) {
                        $count = $obj->where("(status=" . $id . " or status=6) and belong like '" . $ifin . "%'")->count();
                        $p = getPage($count);
                        $list = $obj->where("(status=" . $id . " or status=6) and belong like '" . $ifin . "%'")->order("order_time desc")->limit($p->firstRow, $p->listRows)->select();
                    } else {
                        $count = $obj->where("status=" . $id . " and belong like '" . $ifin . "%'")->count();
                        $p = getPage($count);
                        $list = $obj->where("status=" . $id . " and belong like '" . $ifin . "%'")->order("order_time desc")->limit($p->firstRow, $p->listRows)->select();
                    }

                    $datas = [];
                    foreach ($list as $l) {
                        $uid = $l["uid"];
                        $arr = $obj2->where("uid=" . $uid)->find();
                        $data["name"] = $arr["name"];
                        $data["order_time"] = $l["order_time"];
                        $data["status"] = $l["status"];
                        $data["oid"] = $l["oid"];
                        $data["belong"] = $l["belong"];
                        $datas[] = $data;
                    }
                } elseif ($select == "user") {
                    if ($id == 2) {
                        $datas = $obj2->query("SELECT * FROM user_main a LEFT JOIN order_main b ON a.uid=b.uid WHERE a.name like '" . $ifin . "%' and (b.status=" . $id . " or status=6)");
                    } else {
                        $datas = $obj2->query("SELECT * FROM user_main a LEFT JOIN order_main b ON a.uid=b.uid WHERE a.name like '" . $ifin . "%' and b.status=" . $id);
                    }
                } elseif ($select == "phone") {
                    if ($id == 2) {
                        $datas = $obj2->query("SELECT * FROM user_main a LEFT JOIN order_main b ON a.uid=b.uid WHERE a.phone like '" . $ifin . "%' and (b.status=" . $id . " or status=6)");
                    } else {
                        $datas = $obj2->query("SELECT * FROM user_main a LEFT JOIN order_main b ON a.uid=b.uid WHERE a.phone LIKE '" . $ifin . "%' and b.status=" . $id);
                    }
                }
                $this->assign('datas', $datas);
                $this->assign('status', $status);
                if (isMobile()) {
                    $this->display("orderList2_m");
                } else {
                    $this->display("orderList2");
                }
            }
        }
    }

//预约记录删除
    public function orderDel($id)
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $urecord = M("user_record");
            $data = $urecord->where("id=" . $id)->delete();
            if ($data == false || $data == 0) {
                $this->error('删除失败', '../../orderList');
            } else {
                redirect('../../orderList3');
            }
        }
    }

    //预约指派
    public function orderBelong($phone)
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $obj = M("user_main");
            $datas = $obj->where("role=2")->select();
            $this->assign("datas", $datas);
            $this->assign("phone", $phone);

            if (isMobile()) {
                $this->display("belong_m");
            } else {
                $this->display("belong");
            }
        }
    }

    //派单提交
    public function orderBelong2()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $name = $_POST["select"];
            $phone = $_POST["phone"];

            $obj = M("user_record");
            $data["belong"] = $name;
            $res = $obj->where("phone=" . $phone)->save($data);
            if ($res == false) {
                $this->error("派单失败,请重新派单", "orderList");
            } else {
                $this->success("派单成功", "orderList");
            }
        }
    }

    //订单删除
    public function orderDel2($id)
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $obj = M("order_main");
            $data = $obj->where("omid=" . $id)->delete();
            //删除子订单
            $obj2 = M("order_profile");
            $data2 = $obj2->where("omid=" . $id)->delete();
            if ($data == false || $data == 0) {
                $this->error('删除失败', '../../orderList2');
            } else {
                $this->success('删除成功', '../../orderList2');
            }
        }
    }

    //订单详情
    public function orderDetail($id)
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $obj = M("order_profile");
            $datas = $obj->where("omid=" . $id)->select();
            $this->assign("datas", $datas);
            if (isMobile()) {
                $this->display("orderDetail_m");
            } else {
                $this->display();
            }
        }
    }

    //工厂订单列表
    public function orderList4()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $obj = M("order_profile");
            $obj2 = M("user_main");
            $count = $obj->where("status=1 and belong_gc='" . $user . "'")->count();
            $p = getPage($count);
            $list = $obj->where("status=1 and belong_gc='" . $user . "'")->order("gc_updated desc")->limit($p->firstRow, $p->listRows)->select();
            $datas = [];
            foreach ($list as $l) {
                $uid = $l["uid"];
                $arr = $obj2->where("uid=" . $uid)->find();
                $data["name"] = $arr["name"];
                $data["orderNum2"] = $l["ordernum2"];
                $data["xf_type"] = $l["xf_type"];
                $data["sex"] = $l["sex"];
                $data["num"] = $l["num"];
                $data["order_time"] = $l["order_time"];
                $data["status"] = $l["status"];
                $data["oid"] = $l["oid"];
                $data["belong"] = $l["belong"];
                $data["gc_updated"] = $l["gc_updated"];
                $data["color"] = $l["color"];
                $data["collar_type"] = $l["collar_type"];
                $data["sleeve_type"] = $l["sleeve_type"];
                $data["front_fly"] = $l["front_fly"];
                $data["pocket"] = $l["pocket"];
                $data["type"] = $l["type"];
                $data["descr2"] = $l["descr2"];
                //西服
                $data["xf_num"] = $l["xf_num"];
                $data["xf_color"] = $l["xf_color"];
                $data["lapel"] = $l["lapel"];
                $data["jag"] = $l["jag"];
                $data["sleeve_fork"] = $l["sleeve_fork"];
                $data["xf_pocket"] = $l["xf_pocket"];
                $data["fastener"] = $l["fastener"];
                $data["zuo"] = $l["zuo"];

                $data["pro_id"] = $l["pro_id"];
                $data["uid"] = $uid;
                $datas[] = $data;
            }
            $status = 1;
            $this->assign('status', $status);
            $this->assign('datas', $datas);
            $this->assign("page", $p->show());

            if (isMobile()) {
                $this->display("orderList4_m");
            } else {
                $this->display();
            }
        }
    }

//工厂导出
    public function export()
    {
        $start_date = $_POST["start_date"];
        $end_date = $_POST["end_date"];
        if (isset($start_date) && isset($end_date)) {
            $obj = M("order_profile");
            $obj2 = M("user_main");
            $obj3 = M("user_profile");
            $list = $obj->where("order_time>=UNIX_TIMESTAMP('" . $start_date . "') and order_time<UNIX_TIMESTAMP('" . $end_date . "')")->order("order_time desc")->select();
            $datas = [];
            foreach ($list as $l) {
                $uid = $l["uid"];
                $arr = $obj2->where("uid=" . $uid)->find();
                $data["name"] = $arr["name"];
                //订单信息
                $data["orderNum2"] = $l["ordernum2"];
                $data["xf_type"] = $l["xf_type"];
                $data["sex"] = $l["sex"];

                //衬衣
                $data["num"] = $l["num"];
                $data["color"] = $l["color"];
                $data["collar_type"] = $l["collar_type"];
                $data["sleeve_type"] = $l["sleeve_type"];
                $data["front_fly"] = $l["front_fly"];
                $data["pocket"] = $l["pocket"];
                $data["type"] = $l["type"];
                //西服
                $data["xf_num"] = $l["xf_num"];
                $data["xf_color"] = $l["xf_color"];
                $data["lapel"] = $l["lapel"];
                $data["lapel_width"] = $l["lapel_width"];
                $data["jag"] = $l["jag"];
                $data["sleeve_fork"] = $l["sleeve_fork"];
                $data["xf_pocket"] = $l["xf_pocket"];
                $data["fastener"] = $l["fastener"];
                $data["descr2"] = $l["descr2"];

                $type = $l["pro_id"];
                //量体信息
                $arr2 = $obj3->where("uid=" . $uid . " and type=" . $type)->find();
                $data["length"] = $arr2["length"];
                $data["neckline"] = $arr2["neckline"];
                $data["shoulder"] = $arr2["shoulder"];
                $data["sleeve_long"] = $arr2["sleeve_long"];
                $data["sleeve_width"] = $arr2["sleeve_width"];
                $data["cuff"] = $arr2["cuff"];
                $data["hipline"] = $arr2["hipline"];
                $data["chest"] = $arr2["chest"];
                $data["waistline"] = $arr2["waistline"];
                $data["mid_waist"] = $arr2["mid_waist"];
                $data["lower_hem"] = $arr2["lower_hem"];
                $data["rise"] = $arr2["rise"];
                $data["outside_len"] = $arr2["outside_len"];
                $data["tong_fork"] = $arr2["tong_fork"];
                $data["barre"] = $arr2["barre"];
                $data["leg_width"] = $arr2["leg_width"];
                $data["leg_open"] = $arr2["leg_open"];
                $data["barre"] = $arr2["barre"];
                $data["leg_width"] = $arr2["leg_width"];
                $data["leg_open"] = $arr2["leg_open"];
                $data["skirt_len"] = $arr2["skirt_len"];
                $data["descr"] = $arr2["descr"];
                $datas[] = $data;
            }
            exportexcel($datas, ["客户", "编号", "衣服类型", "性别", "衬衫数量", "衬衫面料", "领型", "袖型", "门襟", "口袋", "衬衫类型", "西服数量", "衬衫面料", "驳头", "驳头宽", "开叉", "袖口叉",
                "西服口袋", "纽扣", "备注",
                "衣长", "领围", "肩宽", "袖长", "袖肥", "袖口", "臀围", "胸围", "腰围", "中腰", "下摆", "立裆",
                "裤长", "通裆", "横裆", "中裆", "脚口", "裙长", "量体说明"], "order");
        }
    }

    //西服订单
    public function orderEdit3()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $phone = $_POST['phone'];
            $xf_color = $_POST["xf_color"];
            $lapel = $_POST["lapel"];//驳头
            $lapel_width = $_POST["lapel_width"];//驳头宽
            $jag = $_POST["jag"];//开叉
            $sleeve_fork = $_POST["sleeve_fork"];//袖口叉
            $xf_pocket = $_POST["xf_pocket"];//口袋
            $fastener = $_POST["fastener"];//纽扣
            $fastenerStr = implode(",", $fastener);
            $descr2 = $_POST["descr2"];
            $lts = $_POST["lts"];
            $xf_num = $_POST["xf_num"];
            $xf_type = $_POST["xf_type"];
            $sex = $_POST["sex"];
            $zuo = $_POST["zuo"];
            $price = I("post.price");
            $dj = I("post.dj");
            if (isset($phone)) {
                $timestamp = time();
                $obj = M("user_main");
                $arr = $obj->where("phone=" . $phone)->find();
                $uid = $arr["uid"];
                //查询
                $orderm = M("order_main");
                $orderp = M("order_profile");
                $data2['uid'] = $uid;
                $data2['order_time'] = $timestamp;
                $data2['pro_id'] = 2;
                $data2['xf_color'] = $xf_color;
                $data2['lapel'] = $lapel;
                $data2['lapel_width'] = $lapel_width;
                $data2["jag"] = $jag;
                $data2["sleeve_fork"] = $sleeve_fork;
                $data2['xf_pocket'] = $xf_pocket;
                $data2['fastener'] = $fastenerStr;
                $data2['descr2'] = $descr2;
                $data2['status'] = 1;
                $data2["belong"] = $lts;
                $data2['xf_num'] = $xf_num;
                $data2["xf_type"] = $xf_type;
                $data2["sex"] = $sex;
                $data2["zuo"] = $zuo;
                $data2["price"] = $price;
                $data2["dj"] = $dj;
                $data2["updated"] = $timestamp;
                $rs3 = $orderm->where("DATE(FROM_UNIXTIME(order_time))=CURDATE() and uid=" . $uid)->find();
                $rs = $orderm->where("DATE(FROM_UNIXTIME(order_time))=CURDATE()")->count();
                $rq = date("Ymd", $timestamp);
                if (empty($rs3)) {
                    $str = $rs + 1;
                    $var = sprintf("%04s", $str);
                    $rs2 = $orderp->where("DATE(FROM_UNIXTIME(order_time))=CURDATE() and uid=" . $uid)->count();
                    $str2 = $rs2 + 1;
                    $var2 = sprintf("%03s", $str2);
                    $data2["orderNum2"] = $rq . $var . "-" . $var2;
                    $data["order_time"] = $timestamp;
                    $data["orderNum"] = $rq . $var;
                    $data["uid"] = $uid;
                    $data["count"] = $xf_num;
                    $data["price_count"] = $price;
                    $data["dj_count"] = $dj;
                    $data["status"] = 1;
                    $data["belong"] = $lts;
                    $data["updated"] = $timestamp;
                    $omid = $orderm->add($data);
                } else {
                    $orderNum = $rs3["ordernum"];
                    $omid = $rs3["omid"];
                    $count = $rs3["count"] + $xf_num;
                    $price_count = $rs3["price_count"] + $price;
                    $dj_count = $rs3["dj_count"] + $dj;
                    $orderm->count = $count;
                    $orderm->price_count = $price_count;
                    $orderm->dj_count = $dj_count;
                    $orderm->updated = $timestamp;
                    $orderm->where("omid=" . $omid)->save();
                    $rs2 = $orderp->where("DATE(FROM_UNIXTIME(order_time))=CURDATE() and uid=" . $uid)->count();
                    $str2 = $rs2 + 1;
                    $var2 = sprintf("%03s", $str2);
                    $data2["orderNum2"] = $orderNum . "-" . $var2;
                }
                $data2["omid"] = $omid;
                $oid = $orderp->add($data2);
                if (!empty($oid)) {
                    $obj2 = M("user_record");
                    $obj2->status = 2;
                    $obj2->where("uid=" . $uid)->save();
                    $desc = "订单添加成功！";
                    $this->assign("desc", $desc);
                    $this->assign("phone", $phone);
                    $this->assign("lts", $lts);

                    if (isMobile()) {
                        $this->display("orderEdit_m");
                    } else {
                        $this->display("orderEdit");
                    }
                } else {
                    $desc = "订单添加失败！";
                    $this->assign("desc", $desc);
                    $this->assign("phone", $phone);
                    $this->assign("lts", $lts);
                    if (isMobile()) {
                        $this->display("orderEdit_m");
                    } else {
                        $this->display("orderEdit");
                    }
                }
            }
        }
    }

    //工厂制作而成状态
    public function orderEnd($id)
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $obj = M("order_main");
            $obj2 = M("order_profile");
            $updated = time();
            //修改子订单
            $obj2->status = 2;
            $obj2->updated = $updated;
            $rs2 = $obj2->where("oid=" . $id)->save();
            if ($rs2 == false) {
                $this->error("提交失败", "../../orderList4");
            } else {
                $arr2 = $obj2->where("oid=" . $id)->find();
                $omid = $arr2["omid"];
                $datas = $obj2->where("omid=" . $omid)->select();
                $data2 = array();
                foreach ($datas as $data) {
                    $data2[] = $data["status"];
                }
                //修改主订单
                $obj->status = 2;
                $obj->updated = $updated;
                $obj->where("omid=" . $omid)->save();
//            $this->success("提交成功", "../../orderList4");
                redirect("../../orderList4");
            }
        }
    }

    //已完成订单列表
    public function endList()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $updated = time();
            $obj = M("order_main");
            $obj2 = M("user_main");
            $obj3 = M("order_profile");
            $user = session("user");
            $role = session("role");
            if ($role == 2) {
                $count = $obj->where("(status = 2 or status=6) and belong='" . $user . "'")->count();
                $p = getPage($count);
                $list = $obj->where("(status = 2 or status=6) and belong='" . $user . "'")->order("order_time desc")->limit($p->firstRow, $p->listRows)->select();
            } else {
                $count = $obj->where("status = 2 or status=6")->count();
                $p = getPage($count);
                $list = $obj->where("status = 2 or status=6")->order("order_time desc")->limit($p->firstRow, $p->listRows)->select();
            }

            $datas = [];
            foreach ($list as $l) {
                $data["uid"] = $l["uid"];
                $arr = $obj2->where("uid=" . $data["uid"])->find();
                $data["name"] = $arr["name"];
                $data["phone"] = $arr["phone"];
                $data["orderNum"] = $l["ordernum"];
                $data["count"] = $l["count"];
                $data["order_time"] = $l["order_time"];
                $seven = $data["order_time"] + 604800;
                $ten = $data["order_time"] + 864000;
                if ($seven < $updated && $updated < $ten) {
                    $data["week"] = 1;//超过7天-10天内
                } elseif ($updated > $ten) {
                    $data["week"] = 2;//超过10天
                }
                $data["status"] = $l["status"];
                $data["omid"] = $l["omid"];
                //查询子订单完工数
                $arr3 = $obj3->where("(status=2 or status=6) and omid=" . $data["omid"])->select();
                $data["zi_count"] = 0;
                foreach ($arr3 as $a) {
                    if ($a["pro_id"] == 1) {
                        $data["zi_count"] += $a["num"];
                    } elseif ($a["pro_id"] == 2) {
                        $data["zi_count"] += $a["xf_num"];
                    }
                }

                //判断是否子订单是否都完成了
                if ($data["count"] == $data["zi_count"]) {
                    $data["isAllEnd"] = 1;
                } else {
                    $data["isAllEnd"] = 2;
                }
                $data["belong"] = $l["belong"];
                $data["belong_gc"] = $l["belong_gc"];
                $data["price_count"] = $l["price_count"];
                $data["dj_count"] = $l["dj_count"];
                $datas[] = $data;
            }
            $status = 2;
            //配送员
            $ps_datas = $obj2->where("role=5")->select();
            $this->assign("status", $status);
            $this->assign('datas', $datas);
            $this->assign('ps_datas', $ps_datas);
            $this->assign("page", $p->show());

            if (isMobile()) {
                $this->display("orderList2_m");
            } else {
                $this->display("orderList2");
            }
        }
    }

    //工厂已完成订单列表
    public function endList_gc()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $obj = M("order_profile");
            $obj2 = M("user_main");
            $user = session("user");
            $count = $obj->where("(status=2 or status=6) and belong_gc='" . $user . "'")->count();
            $p = getPage($count);
            $list = $obj->where("(status=2 or status=6) and belong_gc='" . $user . "'")->order("order_time desc")->limit($p->firstRow, $p->listRows)->select();
            $datas = [];
            foreach ($list as $l) {
                $uid = $l["uid"];
                $arr = $obj2->where("uid=" . $uid)->find();
                $data["name"] = $arr["name"];
                $data["orderNum2"] = $l["ordernum2"];
                $data["xf_type"] = $l["xf_type"];
                $data["sex"] = $l["sex"];
                $data["num"] = $l["num"];
                $data["order_time"] = $l["order_time"];
                $data["status"] = $l["status"];
                $data["oid"] = $l["oid"];
                $data["belong"] = $l["belong"];
                $data["color"] = $l["color"];
                $data["collar_type"] = $l["collar_type"];
                $data["sleeve_type"] = $l["sleeve_type"];
                $data["front_fly"] = $l["front_fly"];
                $data["pocket"] = $l["pocket"];
                $data["type"] = $l["type"];
                $data["descr2"] = $l["descr2"];
                //西服
                $data["xf_num"] = $l["xf_num"];
                $data["xf_color"] = $l["xf_color"];
                $data["lapel"] = $l["lapel"];
                $data["jag"] = $l["jag"];
                $data["sleeve_fork"] = $l["sleeve_fork"];
                $data["xf_pocket"] = $l["xf_pocket"];
                $data["fastener"] = $l["fastener"];
                $data["zuo"] = $l["zuo"];

                $data["pro_id"] = $l["pro_id"];
                $data["uid"] = $uid;
                $datas[] = $data;
            }
            $this->assign('datas', $datas);
            $this->assign("page", $p->show());
            if (isMobile()) {
                $this->display("orderList4_m");
            } else {
                $this->display("orderList4");
            }
        }
    }

    //代配送列表
    public function psList()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $obj = M("order_main");
            $obj2 = M("user_main");
            $user = session("user");
            $role = session("role");
            if ($role == 2) {
                $count = $obj->where("status = 3 and belong='" . $user . "'")->count();
                $p = getPage($count);
                $list = $obj->where("status = 3 and belong='" . $user . "'")->order("ps_time asc")->limit($p->firstRow, $p->listRows)->select();
            } elseif ($role == 5) {
                $count = $obj->where("status = 3 and ps_yuan='" . $user . "'")->count();
                $p = getPage($count);
                $list = $obj->where("status = 3 and ps_yuan='" . $user . "'")->order("ps_time asc")->limit($p->firstRow, $p->listRows)->select();
            } else {
                $count = $obj->where("status = 3")->count();
                $p = getPage($count);
                $list = $obj->where("status = 3")->order("ps_time asc")->limit($p->firstRow, $p->listRows)->select();
            }

            $datas = [];
            foreach ($list as $l) {
                $data["uid"] = $l["uid"];
                $arr = $obj2->where("uid=" . $data["uid"])->find();
                $data["name"] = $arr["name"];
                $data["phone"] = $arr["phone"];
                $data["orderNum"] = $l["ordernum"];
                $data["count"] = $l["count"];
                $data["price_count"] = $l["price_count"];
                $data["dj_count"] = $l["dj_count"];
                $data["order_time"] = $l["order_time"];
                $data["status"] = $l["status"];
                $data["omid"] = $l["omid"];
                $data["belong"] = $l["belong"];
                $data["ps_yuan"] = $l["ps_yuan"];
                $data["belong_gc"] = $l["belong_gc"];
                $data["ps_date"] = $l["ps_date"];
                $data["ps"] = $l["ps"];
                $datas[] = $data;
            }

            $status = 3;
            $this->assign('status', $status);
            $this->assign('datas', $datas);
            $this->assign("page", $p->show());

            if (isMobile()) {
                $this->display("orderList2_m");
            } else {
                $this->display("orderList2");
            }
        }
    }

    //修改订单状态为待配送
    public function order_ps($id)
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $ps_date = $_POST["ps_date"];
            $ps = $_POST["ps"];
            $ps_yuan = $_POST["ps_yuan"];
            $obj = M("order_main");
            $point = substr($ps, 0, 2);
            $str = substr($point, -1);
            if ($str == ":") {
                $point = substr($ps, 0, 1);
            }
            $ps_time = $ps_date . " " . $point . ":00";
            $psTime = strtotime($ps_time);
            $obj->status = 3;
            $obj->ps_date = $ps_date;
            $obj->ps = $ps;
            $obj->ps_yuan = $ps_yuan;
            $obj->ps_time = $psTime;
            $rs = $obj->where("omid=" . $id)->save();
            if ($rs == false) {
                $this->error("配送失败", "../../endList");
            } else {
                $obj2 = M("order_profile");
                $obj2->status = 3;
                $obj2->where("omid=" . $id)->save();
                redirect("../../endList");
            }
        }
    }

    //已配送列表
    public function ps_endList()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $obj = M("order_main");
            $obj2 = M("user_main");
            $user = session("user");
            $role = session("role");
            if ($role == 2) {
                $count = $obj->where("status = 4 and belong='" . $user . "'")->count();
                $p = getPage($count);
                $list = $obj->where("status = 4 and belong='" . $user . "'")->order("updated desc")->limit($p->firstRow, $p->listRows)->select();
            } else {
                $count = $obj->where("status = 4")->count();
                $p = getPage($count);
                $list = $obj->where("status = 4")->order("updated desc")->limit($p->firstRow, $p->listRows)->select();
            }

            $datas = [];
            foreach ($list as $l) {
                $data["uid"] = $l["uid"];
                $arr = $obj2->where("uid=" . $data["uid"])->find();
                $data["name"] = $arr["name"];
                $data["phone"] = $arr["phone"];
                $data["orderNum"] = $l["ordernum"];
                $data["count"] = $l["count"];
                $data["order_time"] = $l["order_time"];
                $data["status"] = $l["status"];
                $data["omid"] = $l["omid"];
                $data["belong"] = $l["belong"];
                $data["belong_gc"] = $l["belong_gc"];
                $data["pay_type"] = $l["pay_type"];
                $data["price_count"] = $l["price_count"];
                $data["dj_count"] = $l["dj_count"];
                $datas[] = $data;
            }
            $status = 4;
            $this->assign('status', $status);
            $this->assign('datas', $datas);
            $this->assign("page", $p->show());

            if (isMobile()) {
                $this->display("orderList2_m");
            } else {
                $this->display("orderList2");
            }
        }
    }

    //修改订单为已配送状态
    public function order_psEnd($id)
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $pay = I("post.pay");
            $updated = time();
            $obj = M("order_main");
            $obj->status = 4;
            $obj->updated = $updated;
            $obj->pay_type = $pay;
            $rs = $obj->where("omid=" . $id)->save();
            if ($rs == false) {
                $this->error("失败", "../../psList");
            } else {
                $obj2 = M("order_profile");
                $obj2->status = 4;
                $obj2->updated = $updated;
                $rs2 = $obj2->where("omid=" . $id)->save();
                if ($rs2 == false) {
                    $this->error("失败", "../../psList");
                } else {
                    redirect("../../psList");
                }
            }
        }
    }

    //修改订单为返工状态
    public function return_order($id)
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $return = $_POST["return"];
            $updated = time();
            $obj = M("order_profile");
            $obj2 = M("order_main");
            $objArr["descr2"] = $return;
            $objArr["status"] = 5;
            $objArr["return_time"] = $updated;
            $rs = $obj->where("oid=" . $id)->save($objArr);
            //保存返工记录
            $proData = $obj->where("oid=" . $id)->find();
            $obj3 = M("order_record");
            $obj3Arr["oid"] = $id;
            $obj3Arr["uid"] = $proData["uid"];
            $obj3Arr["ordertime"] = $proData["order_time"];
            $obj3Arr["ordernum2"] = $proData["ordernum2"];
            $obj3Arr["type"] = $proData["xf_type"];
            if ($proData["pro_id"] == 1) {
                $obj3Arr["num"] = $proData["num"];
            } else {
                $obj3Arr["num"] = $proData["xf_num"];
            }
            $obj3Arr["descr"] = $return;
            $obj3Arr["return_date"] = $updated;
            $obj3Arr["gc"] = $proData["belong_gc"];

            $obj3->add($obj3Arr);

            if ($rs == false) {
                $this->error("失败", "../../psList");
            } else {
                $arr = $obj->where("oid=" . $id)->find();
                $omid = $arr["omid"];
                $datas = $obj->where("omid=" . $omid)->select();
                $om_arr = array();
                foreach ($datas as $data) {
                    $om_arr[] = $data["status"];
                }
                if (in_array("4", $om_arr) == false && in_array("3", $om_arr) == false) {
                    $obj2->status = 5;
                    $obj2->updated = $updated;
                    $obj2->where("omid=" . $omid)->save();
                }
                $this->success("成功", "../../returnList");
            }
        }
    }

    //返工单工厂列表
    public function returnList_gc()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $obj = M("order_profile");
            $obj2 = M("user_main");
            $user = session("user");
            $count = $obj->where("status=5 and belong_gc='" . $user . "'")->count();
            $p = getPage($count);
            $list = $obj->where("status=5 and belong_gc='" . $user . "'")->order("order_time desc")->limit($p->firstRow, $p->listRows)->select();
            $datas = [];
            foreach ($list as $l) {
                $uid = $l["uid"];
                $arr = $obj2->where("uid=" . $uid)->find();
                $data["name"] = $arr["name"];
                $data["orderNum2"] = $l["ordernum2"];
                $data["xf_type"] = $l["xf_type"];
                $data["sex"] = $l["sex"];
                $data["num"] = $l["num"];
                $data["order_time"] = $l["order_time"];
                $data["status"] = $l["status"];
                $data["oid"] = $l["oid"];
                $data["belong"] = $l["belong"];
                $data["color"] = $l["color"];
                $data["collar_type"] = $l["collar_type"];
                $data["sleeve_type"] = $l["sleeve_type"];
                $data["front_fly"] = $l["front_fly"];
                $data["pocket"] = $l["pocket"];
                $data["type"] = $l["type"];
                $data["descr2"] = $l["descr2"];
                //西服
                $data["xf_num"] = $l["xf_num"];
                $data["xf_color"] = $l["xf_color"];
                $data["lapel"] = $l["lapel"];
                $data["jag"] = $l["jag"];
                $data["sleeve_fork"] = $l["sleeve_fork"];
                $data["xf_pocket"] = $l["xf_pocket"];
                $data["fastener"] = $l["fastener"];
                $data["zuo"] = $l["zuo"];
                $data["pro_id"] = $l["pro_id"];
                $data["uid"] = $uid;
                $datas[] = $data;
            }
            $s = 5;
            $this->assign('s', $s);
            $this->assign('datas', $datas);
            $this->assign("page", $p->show());

            if (isMobile()) {
                $this->display("orderList4_m");
            } else {
                $this->display("orderList4");
            }
        }
    }

    //返工单列表
    public function returnList()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $obj = M("order_profile");
            $obj2 = M("user_main");
            $arr = $obj2->where("name='" . $user . "'")->find();
            if ($arr['role'] != 0) {
                $count = $obj->where("status=5 and uid=" . $arr['uid'])->count();
                $p = getPage($count);
                $list = $obj->where("status=5 and uid=" . $arr['uid'])->order("order_time desc")->limit($p->firstRow, $p->listRows)->select();
                $datas = [];
                foreach ($list as $l) {
                    $uid = $l["uid"];
                    $arr = $obj2->where("uid=" . $uid)->find();
                    $data["name"] = $arr["name"];
                    $data["orderNum2"] = $l["ordernum2"];
                    $data["return_time"] = $l["return_time"];
                    $data["xf_type"] = $l["xf_type"];
                    $data["sex"] = $l["sex"];
                    $data["num"] = $l["num"];
                    $data["order_time"] = $l["order_time"];
                    $data["status"] = $l["status"];
                    $data["oid"] = $l["oid"];
                    $data["belong"] = $l["belong"];
                    $data["belong_gc"] = $l["belong_gc"];
                    $data["color"] = $l["color"];
                    $data["collar_type"] = $l["collar_type"];
                    $data["sleeve_type"] = $l["sleeve_type"];
                    $data["front_fly"] = $l["front_fly"];
                    $data["pocket"] = $l["pocket"];
                    $data["type"] = $l["type"];
                    $data["descr2"] = $l["descr2"];
                    //西服
                    $data["xf_num"] = $l["xf_num"];
                    $data["xf_color"] = $l["xf_color"];
                    $data["lapel"] = $l["lapel"];
                    $data["jag"] = $l["jag"];
                    $data["sleeve_fork"] = $l["sleeve_fork"];
                    $data["xf_pocket"] = $l["xf_pocket"];
                    $data["fastener"] = $l["fastener"];
                    $data["zuo"] = $l["zuo"];

                    $data["pro_id"] = $l["pro_id"];
                    $data["uid"] = $uid;
                    $datas[] = $data;
                }
            } else {
                $count = $obj->where("status=5")->count();
                $p = getPage($count);
                $list = $obj->where("status=5")->order("order_time desc")->limit($p->firstRow, $p->listRows)->select();
                $datas = [];
                foreach ($list as $l) {
                    $uid = $l["uid"];
                    $arr = $obj2->where("uid=" . $uid)->find();
                    $data["name"] = $arr["name"];
                    $data["orderNum2"] = $l["ordernum2"];
                    $data["return_time"] = $l["return_time"];
                    $data["xf_type"] = $l["xf_type"];
                    $data["sex"] = $l["sex"];
                    $data["num"] = $l["num"];
                    $data["order_time"] = $l["order_time"];
                    $data["status"] = $l["status"];
                    $data["oid"] = $l["oid"];
                    $data["belong"] = $l["belong"];
                    $data["belong_gc"] = $l["belong_gc"];
                    $data["color"] = $l["color"];
                    $data["collar_type"] = $l["collar_type"];
                    $data["sleeve_type"] = $l["sleeve_type"];
                    $data["front_fly"] = $l["front_fly"];
                    $data["pocket"] = $l["pocket"];
                    $data["type"] = $l["type"];
                    $data["descr2"] = $l["descr2"];
                    //西服
                    $data["xf_num"] = $l["xf_num"];
                    $data["xf_color"] = $l["xf_color"];
                    $data["lapel"] = $l["lapel"];
                    $data["jag"] = $l["jag"];
                    $data["sleeve_fork"] = $l["sleeve_fork"];
                    $data["xf_pocket"] = $l["xf_pocket"];
                    $data["fastener"] = $l["fastener"];
                    $data["zuo"] = $l["zuo"];

                    $data["pro_id"] = $l["pro_id"];
                    $data["uid"] = $uid;
                    $datas[] = $data;
                }
            }

            $s = 5;
            $this->assign('s', $s);
            $this->assign('datas', $datas);
            $this->assign("page", $p->show());

            if (isMobile()) {
                $this->display("orderList4_m");
            } else {
                $this->display("orderList4");
            }
        }
    }

//工厂返工完成状态
    public function fgEnd($id)
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $obj = M("order_main");
            $obj2 = M("order_profile");
            $updated = time();
            //修改子订单
            $obj2->status = 6;
            $obj2->updated = $updated;
            $rs2 = $obj2->where("oid=" . $id)->save();
            if ($rs2 == false) {
                $this->error("提交失败", "../../returnList_gc");
            } else {
                $arr2 = $obj2->where("oid=" . $id)->find();
                $omid = $arr2["omid"];
                $datas = $obj2->where("omid=" . $omid)->select();
                foreach ($datas as $data) {
                    if ($data["status"] == 3 || $data["status"] == 4) {
                        $proArr["status"] = 2;
                        $obj2->where("oid=" . $data["oid"])->save($proArr);
                    }
                }
                //修改主订单
                $obj->status = 6;
                $obj->updated = $updated;
                $obj->where("omid=" . $omid)->save();
                $this->success("提交成功", "../../returnList_gc");
            }
        }
    }

    //修改子订单
    public function xgOrder($id, $type)
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $data = array();
            $data = I("post.");
            //更新数据
            $obj = M("order_profile");
            $arr = $obj->where("oid=" . $id)->find();
            $omid = $arr["omid"];
            $rs = $obj->where("oid=" . $id)->save($data);

            //更新价格
            $obj2 = M("order_main");
            $p_arr = $obj->where("omid=" . $omid)->select();
            $data2["price_count"] = 0;
            $data2["dj_count"] = 0;
            $data2["count"] = 0;
            foreach ($p_arr as $p) {
                $data2["price_count"] += $p["price"];
                $data2["dj_count"] += $p["dj"];
                if ($p["pro_id"] == 1) {
                    $data2["count"] += $p["num"];
                } elseif ($p["pro_id"] == 2) {
                    $data2["count"] += $p["xf_num"];
                }
            }
            $rs2 = $obj2->where("omid=" . $omid)->save($data2);
            if ($rs == false) {
                $this->error("修改失败", "../../../../orderDetail/id/" . $omid);
            } else {
                redirect("../../../../orderDetail/id/" . $omid);
            }
        }
    }

    //编辑裙子订单
    public function skirt_order()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $phone = $_POST['phone'];
            $color = $_POST["color"];
            $jag = $_POST["jag"];
            $sleeve_type = $_POST["sleeve_type"];
            $descr2 = $_POST["descr2"];
            $lts = $_POST["lts"];
            $num = $_POST["num"];
            $sex = "女士";
            $zuo = $_POST["zuo"];
            $price = I("post.price");
            $dj = I("post.dj");
            if (isset($phone)) {
                $timestamp = time();
                $obj = M("user_main");
                $arr = $obj->where("phone=" . $phone)->find();
                $uid = $arr["uid"];
                //查询
                $orderm = M("order_main");
                $orderp = M("order_profile");
                $data2['uid'] = $uid;
                $data2['order_time'] = $timestamp;
                $data2["updated"] = $timestamp;
                $data2["jag"] = $jag;
                $data2['sleeve_type'] = $sleeve_type;
                $data2['pro_id'] = 7;
                $data2['color'] = $color;
                $data2['descr2'] = $descr2;
                $data2['status'] = 1;
                $data2["belong"] = $lts;
                $data2['num'] = $num;
                $data2["sex"] = $sex;
                $data2["zuo"] = $zuo;
                $data2["price"] = $price;
                $data2["dj"] = $dj;
                $rs3 = $orderm->where("DATE(FROM_UNIXTIME(order_time))=CURDATE() and uid=" . $uid)->find();
                $rs = $orderm->where("DATE(FROM_UNIXTIME(order_time))=CURDATE()")->count();
                $rq = date("Ymd", $timestamp);
                if (empty($rs3)) {
                    $str = $rs + 1;
                    $var = sprintf("%04s", $str);
                    $rs2 = $orderp->where("DATE(FROM_UNIXTIME(order_time))=CURDATE() and uid=" . $uid)->count();
                    $str2 = $rs2 + 1;
                    $var2 = sprintf("%03s", $str2);
                    $data2["orderNum2"] = $rq . $var . "-" . $var2;
                    $data["order_time"] = $timestamp;
                    $data["orderNum"] = $rq . $var;
                    $data["uid"] = $uid;
                    $data["count"] = $num;
                    $data["price_count"] = $price;
                    $data["dj_count"] = $dj;
                    $data["status"] = 1;
                    $data["belong"] = $lts;
                    $data["updated"] = $timestamp;
                    $omid = $orderm->add($data);
                } else {
                    $orderNum = $rs3["ordernum"];
                    $omid = $rs3["omid"];
                    $count = $rs3["count"] + $num;
                    $price_count = $rs3["price_count"] + $price;
                    $dj_count = $rs3["dj_count"] + $dj;
                    $orderm->count = $count;
                    $orderm->price_count = $price_count;
                    $orderm->dj_count = $dj_count;
                    $orderm->updated = $timestamp;
                    $orderm->where("omid=" . $omid)->save();
                    $rs2 = $orderp->where("DATE(FROM_UNIXTIME(order_time))=CURDATE() and uid=" . $uid)->count();
                    $str2 = $rs2 + 1;
                    $var2 = sprintf("%03s", $str2);
                    $data2["orderNum2"] = $orderNum . "-" . $var2;
                }
                $data2["omid"] = $omid;
                $oid = $orderp->add($data2);
                if (!empty($oid)) {
                    $obj2 = M("user_record");
                    $obj2->status = 2;
                    $obj2->where("uid=" . $uid)->save();
                    $desc = "订单添加成功！";
                    $this->assign("desc", $desc);
                    $this->assign("phone", $phone);
                    $this->assign("lts", $lts);
                    $this->display("orderEdit");
                } else {
                    $desc = "订单添加失败！";
                    $this->assign("desc", $desc);
                    $this->assign("phone", $phone);
                    $this->assign("lts", $lts);

                    if (isMobile()) {
                        $this->display("orderEdit_m");
                    } else {
                        $this->display("orderEdit");
                    }
                }
            }
        }
    }

    //直接下单
    public function zj_order($id)
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $obj = M("user_main");
            $obj2 = M("user_profile");
            $arr = $obj->where("uid=" . $id)->find();
            $phone = $arr["phone"];
            $arr2 = $obj2->where("uid=" . $id)->find();
            $lts = $arr2["lts"];
            if (empty($lts)) {
                $arr3 = $obj2->where("uid=" . $id)->select();
                $lts = $arr3[1]["lts"];
            }
            $this->assign("phone", $phone);
            $this->assign("lts", $lts);
            if (isMobile()) {
                $this->display("orderEdit_m");
            } else {
                $this->display("orderEdit");
            }
        }
    }

    //分配工厂后的订单列表
    public function orderList2_1()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $obj = M("order_main");
            $obj2 = M("user_main");
            $user = session("user");
            $role = session("role");
            if ($role == 2) {
                $count = $obj->where("status = 1 and belong_gc is not null and belong='" . $user . "'")->count();
                $p = getPage($count);
                $list = $obj->where("status = 1 and belong_gc is not null and belong='" . $user . "'")->order("gc_updated desc")->limit($p->firstRow, $p->listRows)->select();
            } else {
                $count = $obj->where("status = 1 and belong_gc is not null ")->count();
                $p = getPage($count);
                $list = $obj->where("status = 1 and belong_gc is not null ")->order("gc_updated desc")->limit($p->firstRow, $p->listRows)->select();
            }

            $datas = [];
            foreach ($list as $l) {
                $data["uid"] = $l["uid"];
                $arr = $obj2->where("uid=" . $data["uid"])->find();
                $data["name"] = $arr["name"];
                $data["phone"] = $arr["phone"];
                $data["orderNum"] = $l["ordernum"];
                $data["count"] = $l["count"];
                $data["price_count"] = $l["price_count"];
                $data["dj_count"] = $l["dj_count"];
                $data["order_time"] = $l["order_time"];
                $data["status"] = $l["status"];
                $data["omid"] = $l["omid"];
                $data["belong"] = $l["belong"];
                $data["belong_gc"] = $l["belong_gc"];
                $data["gc_updated"] = $l["gc_updated"];
                $datas[] = $data;
            }
            $status = 1;
            $this->assign('status', $status);
            $this->assign('datas', $datas);
            $this->assign("page", $p->show());
            if (isMobile()) {
                $this->display("orderList2_m");
            } else {
                $this->display("orderList2");
            }
        }
    }

    //分配工厂页面
    public function orderBelongGc($id)
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $obj = M("user_main");
            $datas = $obj->where("role=4")->select();
            $this->assign("datas", $datas);
            $this->assign("omid", $id);
            if (isMobile()) {
                $this->display("belong_m");
            } else {
                $this->display("belong");
            }
        }
    }

    //分配工厂提交
    public function orderBelongGc2()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $gc_updated = time();
            $data = I("post.");
            $obj = M("order_main");
            $obj2 = M("order_profile");
            $objArr["belong_gc"] = $data["gc"];
            $objArr["gc_updated"] = $gc_updated;
            $rs = $obj->where("omid=" . $data["omid"])->save($objArr);

            $obj2Arr["belong_gc"] = $data["gc"];
            $obj2Arr["gc_updated"] = $gc_updated;
            $rs2 = $obj2->where("omid=" . $data["omid"])->save($obj2Arr);

            if ($rs == false || $rs2 == false) {
                $this->error("失败", "orderList2");
            } else {
                redirect("orderList2");
            }
        }
    }

    //完成配送选择付款方式
    public function ps_pay($id)
    {
        $pay = 1;
        $this->assign("pay", $pay);
        $this->assign("omid", $id);
        $this->display("belong");
    }

    //代配送打印
    public function ps_print($id)
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $obj = M("order_main");
            $obj2 = M("user_main");
            $print_arr = $obj->where("omid=" . $id)->find();
            $uid = $print_arr["uid"];
            $print_arr2 = $obj2->where("uid=" . $uid)->find();
            $this->assign("print_arr", $print_arr);
            $this->assign("print_arr2", $print_arr2);
            $this->display("Print/printOrder");
        }
    }

    //已配送导出excel页面
    public function endListExport()
    {
        $this->display();
    }

    //已配送导出excel
    public function endListExport2()
    {
        $postData = I("post.");
        $obj = M("order_profile");
        $obj2 = M("user_main");
        $list = $obj->where("order_time>=UNIX_TIMESTAMP('" . $postData["start_time"] . "') and order_time<UNIX_TIMESTAMP('" . $postData["end_time"] . "')")->order("order_time desc")->select();
        $datas = [];
        foreach ($list as $l) {
            $uid = $l["uid"];
            $arr = $obj2->where("uid=" . $uid)->find();
            $data["name"] = $arr["name"];
            //订单信息
            $data["orderNum2"] = $l["ordernum2"];
            $data["xf_type"] = $l["xf_type"];

            //衬衣和西服
            if ($l["pro_id"] == 2) {
                $data["num"] = $l["xf_num"];
                $data["color"] = $l["xf_color"];
                $xf_color = $l["xf_color"];
            } else {
                $data["num"] = $l["num"];
                $data["color"] = $l["color"];
                $csh_color = $l["color"];
            }
            $data["price"] = $l["price"];
            $data["dj"] = $l["dj"];
            $datas[] = $data;
        }
        exportexcel($datas, ["客户", "编号", "衣服类型", "数量", "面料", "价格", "订金"], "orderEndPrice");
    }

    //返工记录导出页面
    public function returnOrderExport()
    {
        $status = 5;
        $this->assign("status", $status);
        $this->display("endListExport");
    }

    //返工记录导出
    public function returnOrderExport2()
    {
        $postData = I("post.");
        $obj = M("order_profile");
        $obj2 = M("user_main");
        $list = $obj->where("order_time>=UNIX_TIMESTAMP('" . $postData["start_time"] . "') and order_time<UNIX_TIMESTAMP('" . $postData["end_time"] . "')")->order("order_time desc")->select();
        $datas = [];
        foreach ($list as $l) {
            $uid = $l["uid"];
            $arr = $obj2->where("uid=" . $uid)->find();
            $data["name"] = $arr["name"];
            //订单信息
            $data["orderNum2"] = $l["ordernum2"];
            $data["xf_type"] = $l["xf_type"];

            //衬衣和西服
            if ($l["pro_id"] == 2) {
                $data["num"] = $l["xf_num"];
                $data["color"] = $l["xf_color"];
                $xf_color = $l["xf_color"];
            } else {
                $data["num"] = $l["num"];
                $data["color"] = $l["color"];
                $csh_color = $l["color"];
            }
            $data["price"] = $l["price"];
            $data["dj"] = $l["dj"];
            $data["descr2"] = $l["descr2"];
            $datas[] = $data;
        }
        exportexcel($datas, ["客户", "编号", "衣服类型", "数量", "面料", "价格", "订金", "返工理由"], "returnOrder");
    }

    //订单分配配送员页面
    public function psOrderBelong($id)
    {
        $obj = M("user_main");
        $datas = $obj->where("role=5")->select();
        $this->assign("datas", $datas);
        $this->assign("omid", $id);
        $this->display();
    }

    //订单分配配送员
    public function psOrderBelong2()
    {
        $data = I("post.");
        $obj = M("order_main");
        $arr["ps_yuan"] = $data["ps_yuan"];
        $rs = $obj->where("omid=" . $data["omid"])->save($arr);
        if ($rs == false) {
            $this->error("分配失败", "../Order/psList");
        } else {
            redirect("../Order/psList");
        }
    }

    //返工单修改
    public function fgOrderEdit($id)
    {
        $obj = M("order_profile");
        $data = $obj->where("oid=" . $id)->find();
        $this->assign("data", $data);
        $this->display();
    }

    //冲单订单，订单拆分
    public function chaOrderEdit2()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            $this->display("User/index");
        } else {
            $phone = $_POST['phone'];
            $color = $_POST["color"];
            $collar_type = $_POST["collar_type"];
            $sleeve_type = $_POST["sleeve_type"];
            $front_fly = $_POST["front_fly"];
            $pocket = $_POST["pocket"];
            $select = $_POST["select"];
            $descr2 = $_POST["descr2"];
            $lts = $_POST["lts"];
            $num = $_POST["num"];
            $xf_type = $_POST["xf_type"];
            $sex = $_POST["sex"];
            $zuo = $_POST["zuo"];
            $price = I("post.price");
            $dj = I("post.dj");
            if (isset($phone)) {
                $timestamp = time();
                $obj = M("user_main");
                $arr = $obj->where("phone=" . $phone)->find();
                $shj = $obj->where("name='" . $user . "'")->find();
                $uid = $arr["uid"];
                //查询
                $orderm = M("order_main");
                $orderp = M("order_profile");
//                $orderPay = M("order_money");
                $data2['uid'] = $uid;
                $data2['order_time'] = $timestamp;
                $data2['pro_id'] = 1;
                $data2['color'] = $color;
                $data2['collar_type'] = $collar_type;
                $data2['sleeve_type'] = $sleeve_type;
                $data2["front_fly"] = $front_fly;
                $data2["pocket"] = $pocket;
                $data2['type'] = $select;
                $data2['descr2'] = $descr2;
                $data2['status'] = 1;
                $data2["belong"] = $lts;
                $data2['num'] = $num;
                $data2["xf_type"] = $xf_type;
                $data2["sex"] = $sex;
                $data2["zuo"] = $zuo;
                $data2["price"] = $price;
                $data2["dj"] = $dj;
                $data2["updated"] = $timestamp;

//                $rs3 = $orderm->where("DATE(FROM_UNIXTIME(order_time))=CURDATE() and uid=" . $uid)->find();
                $rs = $orderm->where("DATE(FROM_UNIXTIME(order_time))=CURDATE()")->count();
                $rq = date("Ymd", $timestamp);

                if ($rs == 0) {
                    $str = $rs + 1;
                } else {
                    $order = $orderm->where("DATE(FROM_UNIXTIME(order_time))=CURDATE()")->order("order_time desc")->find();
                    $orderStr = $order["ordernum"];
                    $numStr = substr($orderStr, -4);
                    $str = (int)$numStr + 1;
                }

                $var = sprintf("%04s", $str);
                $rs2 = $orderp->where("DATE(FROM_UNIXTIME(order_time))=CURDATE() and uid=" . $uid)->count();
                $str2 = $rs2 + 1;
                $var2 = sprintf("%03s", $str2);
                $data2["orderNum2"] = $rq . $var . "-" . $var2;
                $data["order_time"] = $timestamp;
                $data["orderNum"] = $rq . $var;
                $data["uid"] = $uid;
                $data["count"] = $num;
                $data["price_count"] = $price;
                $data["dj_count"] = $dj;
                $data["status"] = 1;
                $data["belong"] = $lts;
                $data["updated"] = $timestamp;
                $data['rel'] = $shj['uid'];
                $omid = $orderm->add($data);

                $data2["omid"] = $omid;
                $oid = $orderp->add($data2);
                $data3['oid'] = $oid;
                $data3['omid'] = $omid;
                $data3['pay_status'] = 1;
//                $orderPay->add($data3);
                if (!empty($oid)) {
                    $obj2 = M("user_record");
                    $obj2->status = 2;
                    $obj2->where("uid=" . $uid)->save();
                    $desc = "订单添加成功！";
                    $this->assign("desc", $desc);
                    $this->assign("phone", $phone);
                    $this->assign("lts", $lts);
                    $this->display("orderEdit");
                } else {
                    $desc = "订单添加失败！";
                    $this->assign("desc", $desc);
                    $this->assign("phone", $phone);
                    $this->assign("lts", $lts);

                    if (isMobile()) {
                        $this->display("orderEdit_m");
                    } else {
                        $this->display("orderEdit");
                    }
                }
            }
        }
    }

    /*
     * 商家客户管理
     * @wan
     * */
    public function shopCustomer()
    {
        $uid = session("uid");
        if ($uid == null || $uid == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
                return;
            } else {
                $this->display("User/index");
                return;
            }
        }
        $shop = M("shop_info");
        $order = M("order_main");
        $user = M("user_main");
//        $res = $user->where("uid=" . $uid)->find();
        $userArr = $shop->where("uid=" . $uid)->find();
        if (empty($userArr)) {
            $this->assign("code", 4002);
            $this->assign("desc", "您还没有店铺，赶紧创建吧");
            $this->display("shopOrderList");
            return;
        }
        $orderArr = $order->where("shop_id=" . $userArr['shop_id'])->select();
        if (empty($orderArr)) {
            $this->assign("code", 4002);
            $this->assign("desc", "您还没有客户");
        } else {
            $datas = [];
            foreach ($orderArr as $o) {
                $res = $user->where("uid=" . $o['uid'])->find();//查询用户信息
                $info['name'] = $res['name'];
                $info['phone'] = $res['phone'];
                $datas[] = $info;
            }
            $this->assign("datas", $datas);
        }
        $this->display();
    }

}